package com.base.web.auth.service.impl;

import com.base.web.auth.service.ResourceService;
import com.base.web.core.basic.service.BasicServiceImpl;
import com.base.web.dao.master.auth.ResourceMapper;
import com.base.web.model.master.auth.MenuTree;
import com.base.web.model.master.auth.ResourceBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @Author: rentie
 * @Date: 2018/7/9 15:32
 */
@Service
public class ResourceServiceImpl extends BasicServiceImpl<ResourceBean> implements ResourceService {

    @Resource
    private ResourceMapper resourceMapper;

    @Override
    @CacheEvict(value = "menus",allEntries = true)
    public Integer insertSelective(ResourceBean record) {
        return super.insertSelective(record);
    }

    @Override
    @CacheEvict(value = "menus",allEntries = true)
    public Integer updateByPrimaryKeySelective(ResourceBean record) {
        return super.updateByPrimaryKeySelective(record);
    }

    @Override
    @Cacheable(value = "menus",key="'menuSelect'")
    public List<ResourceBean> select(ResourceBean record) {
        return super.select(record);
    }

    @Override
    public PageInfo<ResourceBean> queryManageList(Map<String, String> map) {
        Integer currentPage = Integer.valueOf(map.get("page"));
        Integer pageSize = Integer.valueOf(map.get("limit"));
        PageHelper.startPage(currentPage, pageSize);
        List<ResourceBean> list = resourceMapper.queryManageList();
        return new PageInfo<ResourceBean>(list);
    }


    /**
     * 用户菜单列表
     *
     * @param id 用户id
     * @return
     */
    @Override
    @Cacheable(value = "menus",key="'userMenu_'+#p0")
    public TreeSet<MenuTree> queryUserMenuListById(String id) {
        TreeSet<MenuTree> result = new TreeSet<>();

        //一级菜单
        List<ResourceBean> firstList = resourceMapper.queryMenuListByParentId("0", id);

        if (firstList != null && firstList.size() > 0) {
            firstList.forEach(f -> {

                MenuTree first = new MenuTree(f.getName(), f.getUrl(), f.getIcon(), f.getResOrder());
                //二级菜单
                List<ResourceBean> secondList = resourceMapper.queryMenuListByParentId(f.getId(), id);

                if (secondList != null && secondList.size() > 0) {
                    TreeSet<MenuTree> secondChilds = new TreeSet<>();
                    secondList.forEach(s -> {
                        MenuTree second = new MenuTree(s.getName(), s.getUrl(), s.getIcon(), s.getResOrder());
                        //三级菜单
                        List<ResourceBean> thirdList = resourceMapper.queryMenuListByParentId(s.getId(), id);
                        if (thirdList != null && thirdList.size() > 0) {
                            TreeSet<MenuTree> thirdChilds = new TreeSet<>();
                            thirdList.forEach(t -> {
                                MenuTree third = new MenuTree(t.getName(), t.getUrl(), t.getIcon(), t.getResOrder());
                                thirdChilds.add(third);
                            });
                            second.setData(thirdChilds);
                        }
                        secondChilds.add(second);
                    });
                    first.setData(secondChilds);
                }
                result.add(first);
            });
        }
        return result;
    }

    /**
     * 所有菜单
     *
     * @param parentId 父级id
     * @return
     */
    @Override
    @Cacheable(value = "menus",key="'menuTree'")
    public List<MenuTree> queryMenuList(String parentId) {
        List<MenuTree> result = new ArrayList<>();

        //一级菜单
        List<ResourceBean> firstList = resourceMapper.queryMenuList(parentId);

        if (firstList != null) {
            firstList.forEach(f -> {

                MenuTree first = new MenuTree(f.getName(), f.getId());
                //二级菜单
                List<ResourceBean> secondList = resourceMapper.queryMenuList(f.getId());

                if (secondList != null) {
                    List<MenuTree> secondChilds = new ArrayList<>();
                    secondList.forEach(s -> {
                        MenuTree second = new MenuTree(s.getName(), s.getId());
                        //三级菜单
                        List<ResourceBean> thirdList = resourceMapper.queryMenuList(s.getId());
                        if (thirdList != null) {
                            List<MenuTree> thirdChilds = new ArrayList<>();
                            thirdList.forEach(t -> {
                                MenuTree third = new MenuTree(t.getName(), t.getId());
                                thirdChilds.add(third);
                            });
                            second.setData(thirdChilds);
                        }
                        secondChilds.add(second);
                    });
                    first.setData(secondChilds);
                }
                result.add(first);
            });
        }
        return result;
    }


}